from system.object import Object
from system.eventServer import eventServer

class Frame(Object):
    def __init__(self, parent, type = None, name = None):
        Object.__init__(self, parent, type, name)
        
        self.defaultVar("renderOp2d","rect")
        self.defaultVar("color",(0.5,0.5,0.5))
        self.defaultVar("barColor",(0.75,0.75,0.75))
        self.defaultVar("barSize",0.1)
        self.defaultVar("resizeBoxSize",0.05)
        self.setVar("state","idle")
        self.setVar("closed",False)
        
        self.bar = self.addChild("bar")
        self.bar.setVar("renderOp2d","rect")
        self.bar.setVar("color",self.getVar("barColor"))
        self.bar.setVar("scale",(1,self.getVar("barSize")))
        
        self.outline = self.addChild("outline")
        self.outline.setVar("renderOp2d","rect")
        self.outline.setVar("filled",False)
        self.outline.setVar("color",(1,1,1))
        
        if self.hasVar("title"):
            self.text = self.addChild("text")
            self.text.setVar("renderOp2d","text")
            self.text.setVar("pos",(0,self.getVar("barSize")))
            self.text.setVar("text",self.getVar("title"))
            
        if self.hasVar("canClose") and self.getVar("canClose"):
            self.makeCloseButton()
        else:
            self.closeButton = None
            
        eventServer.register("objectClickDown",self.objectClickDown)
        eventServer.register("objectClickUp",self.mouseButtonUp)
        eventServer.register("mouseButtonUp",self.mouseButtonUp)
        eventServer.register("mouseButtonDown",self.mouseButtonDown)
        eventServer.register("mouseMotion",self.mouseMotion)
        
        self.offx = self.offy = 0
        
    def makeCloseButton(self):
        self.closeButton = self.bar.addChild("closeButton")
        self.closeButton.setVar("renderOp2d","rect")
        self.closeButton.setVar("pos",(0.92,0.2))
        self.closeButton.setVar("scale",(0.06,0.6))
        self.closeButton.setVar("color",self.getVar("color"))
        
        outline = self.closeButton.addChild("outline")
        outline.setVar("renderOp2d","rect")
        outline.setVar("filled",False)
        
        l1 = 0.2
        l2 = 0.8
        
        line = self.closeButton.addChild("line")
        line.setVar("renderOp2d","line")
        line.setVar("points",[(l1,l1),(l2,l2)])
        
        line = self.closeButton.addChild("line")
        line.setVar("renderOp2d","line")
        line.setVar("points",[(l1,l2),(l2,l1)])
        
    def objectClickDown(self, cmd, args):
        if self.getVar("state") == "idle" and args['button'] == 1 and args['path'] == self.bar.getVar("path"):
            self.setVar("state","moving")
            
            self.outline.setVar("color",(1,0,0))
            
            x,y = self.getVar("pos")
            self.offx = args['x'] - x
            self.offy = args['y'] - y
            
        if self.closeButton and args['path'] == self.closeButton.getVar("path"):
            self.log("close")
            self.parent.removeChild(self.getVar("name"))
            self.setVar("closed",True)
            
    def mouseButtonUp(self, cmd, args):
        if args['button'] == 1:
            self.setVar("state","idle")
            self.outline.setVar("color",(1,1,1))
            
    def mouseButtonDown(self, cmd, args):
        if args['button'] == 1 and self.getVar("state") == "idle":
            x,y = self.getVar("pos")
            sx, sy = self.getVar("scale")
            size = self.getVar("resizeBoxSize")
            dx = abs(args['x'] - (x + sx))
            dy = abs(args['y'] - (y + sy))
            
            if dx < size and dy < size:
                self.setVar("state","resizing")
                self.outline.setVar("color",(1,1,0))
            
    def mouseMotion(self, cmd, args):
        if self.getVar("state") == "moving":
            self.setVar("pos",(args['x'] - self.offx,args['y'] - self.offy))
            
        elif self.getVar("state") == "resizing":
            x,y = self.getVar("pos")
            self.setVar("scale",(args['x'] - x, args['y'] - y))
